package com.test.deencredit.dialog_deencredit;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.test.deencredit.R;
import com.test.deencredit.adapter_deencredit.AddressListAdapter_deencredit;
import com.test.deencredit.util_deencredit.AddsUtl_Tanz_deencredit;
import com.test.deencredit.util_deencredit.http_deencredit.HttpUtil_deencredit;

import java.util.Arrays;
import java.util.List;


public class AddressSelectDialog_Tanzan_deencredit extends BottomSheetDialog {

    private Context context_deencredit;

    private TextView titleTv_deencredit;
    private ListView listView_deencredit;

    private TextView textView_deencredit;

    private StringBuilder addressStrB_deencredit = new StringBuilder();

    private String[] addressStrs_deencredit = new String[2];

    private final OnAddressCheckFinishListener_deencredit onAddressCheckFinishListener_deencredit;

    private final String page_deencredit;

    public AddressSelectDialog_Tanzan_deencredit(@NonNull Context context, TextView textView, OnAddressCheckFinishListener_deencredit onAddressCheckFinishListener, String page) {
        super(context, R.style.Theme_DeenCredit_BottomDialogStyle);
        this.context_deencredit = context;
        this.textView_deencredit = textView;
        this.onAddressCheckFinishListener_deencredit = onAddressCheckFinishListener;
        this.page_deencredit = page;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_address_select_deencredit);
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        initView_deencredit();
    }

    @SuppressLint({"SetTextI18n", "ResourceAsColor"})
    private void initView_deencredit(){

        titleTv_deencredit = findViewById(R.id.das_tv_title);
        listView_deencredit = findViewById(R.id.das_lv_list);

        findViewById(R.id.das_ib_close).setOnClickListener(v -> dismiss());

        //选择市
        titleTv_deencredit.setText("City");
        List<String> stateList = Arrays.asList(AddsUtl_Tanz_deencredit.getInstance_deencredit().getStates_deencredit());
        listView_deencredit.setAdapter(new AddressListAdapter_deencredit(getContext(),stateList));

        listView_deencredit.setOnItemClickListener((parent, view, position, id) -> {
            //保存市
            String city = stateList.get(position);
            addressStrB_deencredit.append(city).append(" ");
            addressStrs_deencredit[0] = city;

            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"CITY_CLICK",city);

            //选择区
            titleTv_deencredit.setText("Area");
            List<String> cityList =Arrays.asList(AddsUtl_Tanz_deencredit.getInstance_deencredit().getCities_deencredit(city));
            listView_deencredit.setAdapter(new AddressListAdapter_deencredit(getContext(),cityList));

            listView_deencredit.setOnItemClickListener((parent1, view1, position1, id1) -> {

                //保存区
                String area = cityList.get(position1);
                addressStrB_deencredit.append(area);
                addressStrs_deencredit[1] = area;

                HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"AREA_CLICK",area);

                //写入地址
                textView_deencredit.setText(addressStrB_deencredit.toString());

                onAddressCheckFinishListener_deencredit.onFinish(addressStrs_deencredit);

                dismiss();

            });

        });
    }

    public interface OnAddressCheckFinishListener_deencredit {
        void onFinish(String[] addressStrs);
    }
}
